﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace SpaceInvaders
{
    public class Jogador : Icon
    {
        float velocity;
        Vector2 screenSize;
        public static Rectangle collisionRect { get; set; }
        public static int lifes = 3;
        int delay = 25;
        int timer = 0;
        bool up;

        public Jogador(ContentManager content, GraphicsDeviceManager graphics)
            : base(content.Load<Texture2D>(@"Imagens\Player"), new Vector2(graphics.PreferredBackBufferWidth / 2, (float)(graphics.PreferredBackBufferHeight - 100))) // 2) * 1.6f))
        {
            velocity = 5;
            screenSize = new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
        }

        public void Update(KeyboardState kState, GameTime gameTime)
        {
            timer++;

            if (kState.IsKeyDown(Keys.Left))
            {
                if (position.X > 0)
                    position.X -= velocity;
            }
            if (kState.IsKeyDown(Keys.Right))
            {
                if (position.X <= screenSize.X - texture.Width)
                    position.X += velocity;
            }

            if (kState.IsKeyDown(Keys.Space))
            {
                if (up)
                {
                    if (timer > (delay / 2))
                    {
                        up = false;
                        timer = 0;
                        GerenciadorDeTiro.Add(position.X, position.Y, texture.Width, texture.Height, false);
                    }
                }
                
                if (timer >= delay)
                {
                    timer = 0;
                    GerenciadorDeTiro.Add(position.X, position.Y, texture.Width, texture.Height, false);
                }
            }
            else if (kState.IsKeyUp(Keys.Space))
            {
                up = true;   
            }

            collisionRect = new Rectangle((int)position.X, (int)position.Y, (int)texture.Width, (int)texture.Height);
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
                spriteBatch.Draw(texture, position, color);
        }

    }
}
